package com.txl.tvluancher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

public class HomeActivity extends FragmentActivity {
    private WebFragment webFragment;
    private NetChangeBroadCastReciver netChangeBroadCastReciver;
    private PackageBroadCastReceiver packageBroadCastReceiver;
    private static HomeActivity activity;
    public static Activity getActivity(){
        return activity;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);
        this.activity = this;
        webFragment = (WebFragment) getSupportFragmentManager().findFragmentById(R.id.webFragment);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netChangeBroadCastReciver = new NetChangeBroadCastReciver();
        registerReceiver(netChangeBroadCastReciver,intentFilter);

        IntentFilter intentFilter1 = new IntentFilter();
        intentFilter1.addAction("android.intent.action.PACKAGE_ADDED");
        intentFilter1.addAction("android.intent.action.PACKAGE_REPLACED");
        intentFilter1.addAction("android.intent.action.PACKAGE_REMOVED");
        packageBroadCastReceiver = new PackageBroadCastReceiver();
        registerReceiver(packageBroadCastReceiver,intentFilter1);
    }

    @SuppressLint("RestrictedApi")
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK ) {
            //桌面程序需要禁用返回
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(netChangeBroadCastReciver);
        unregisterReceiver(packageBroadCastReceiver);
        super.onDestroy();
    }
}
